<template>
  <el-dialog :close-on-click-modal='false' title="商品分类" :visible.sync="dialogFormVisible" :before-close="cancel">
    <el-form ref="form" :rules="rules" :model="form" label-width="120px">
      <el-row>
        <el-col :span="24">
          <el-form-item label="商品分类：" prop="productTypeName">
            <el-input v-model="form.productTypeName" placeholder="请输入商品分类"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="商品备注：" prop="remarks">
            <el-input v-model="form.remarks" placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <!-- <el-form-item label="是否展示：" prop="visible">
            <el-select v-model="form.visible" placeholder="请选择">
              <el-option label="是" :value="'1'" />
              <el-option label="否" :value="'0'" />
            </el-select>
            <span style="font-size: 12px;">app左侧导航</span>
          </el-form-item> -->
          <!-- <el-form-item label="是否展示：" prop="homePageDisplay">
            <el-select v-model="form.homePageDisplay" placeholder="请选择">
              <el-option label="是" :value="true" />
              <el-option label="否" :value="false" />
            </el-select>
            <span style="font-size: 12px;">app首页菜单</span>
          </el-form-item> -->
          <el-form-item label="分类图标：" prop="iconUrl">
            <el-upload class="avatar-uploader" action="" :show-file-list="false" :auto-upload="false"
              :on-change="handleChangeUpload" :on-remove="handleRemove" accept=".png,.jpg,.jpeg">
              <img v-if="form.iconUrl" :src="form.iconUrl" class="avatar">
              <!-- <i v-if="form.iconUrl" class="el-icon-delete avatar-uploader-icon"></i> -->
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
            <span v-if="form.iconUrl" @click="handleRemove"
              style="font-size: 12px;color: rgb(8, 188, 35); text-align: left;cursor: pointer;">
              删除</span>
            <span @click="handleReset" style="font-size: 12px;color: rgb(2, 188, 250); text-align: left;cursor: pointer;">
              重置</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import fileApi from '@/api/module/file'
import config from '@/api/config'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      required: false
    },
    api: {
      type: Function,
      required: true
    }
  },
  data () {
    return {
      dialogFormVisible: false,
      rules: {
        productTypeName: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ],
        remarks: [
          { required: true, message: '请输入备注', trigger: 'blur' }
        ],
        visible: [
          { required: true, message: '请选择是否展示', trigger: 'change' }
        ],
        // homePageDisplay: [
        //   { required: true, message: '请选择是否展示', trigger: 'change' }
        // ]
      },
      resetImg: '',
      form: {
        productTypeName: '',
        homePageDisplay: false,
        visible: '1',
        remarks: '',
        file: '',
        iconUrl: ''
      },
    };
  },
  watch: {
    showDialog: {
      handler (v) {
        console.log(this.api)
        this.dialogFormVisible = v
        v && (this.form = { ...this.form, ...this.formData })
        this.resetImg = this.formData.iconUrl
      }
    }
  },
  methods: {
    //关闭
    close (isRef) {
      this.$emit('closeDialog', isRef)
      this.$refs.form.resetFields()
    },
    //取消
    cancel () {
      this.form = {
        productTypeName: '',
        remarks: '',
        homePageDisplay: false,
        visible: '1',
        file: '',
        iconUrl: ''
      }
      this.close()
    },
    handleRemove (file) {
      this.form.iconUrl = ''
    },
    handleReset () {
      this.form.iconUrl = this.resetImg
    },
    async handleChangeUpload (file) {
      console.log(file)
      let fileId = await this.getFileId(file)
      this.form.iconUrl = config.webServiceUrl + '/ftp/file/download?fileId=' + fileId
      console.log(fileId, '---')
    },
    async getFileId (file) {
      let fileId = ''
      if (file.raw) {
        let fd = new FormData();
        console.log(file)
        fd.append("file", file.raw)
        console.log(fd, 'fd')
        await fileApi.fileUpload({
          fd, moduleType: 'SCIENCE_ARTICLE'
        }).then(res => {
          fileId = res.data.fileId
        }).catch(err => {
          console.log('出错了', err)
        });
      }
      return fileId
    },
    //确认
    confirm () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.form.createUserId = sessionStorage.getItem('userId')
          //  this.api 添加编辑接口 （注意传参）
          this.api(this.form).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.cancel()
              this.close(true)
            } else {
              this.$message({
                type: 'info',
                message: res.msg || '操作失败!'
              });
            }
          })
        } else {
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .avatar-uploader {
  line-height: 0;
}

::v-deep .el-upload {
  width: 100px;
  height: 100px;
  line-height: 100px;
  border: 1px dotted #cecede;
  border-radius: 4px;

  img {
    width: 100%;
    height: 100%;
  }
}
</style>